<template>
  <div class="app-container">
    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="样本编码" prop="simpleCode">
        <el-input v-model="queryParams.simpleCode" placeholder="请输入样本编码" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="主体名称" prop="marketInfoName">
        <el-input v-model="queryParams.marketInfoName" placeholder="请输入主体名称" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="样本类型">
        <el-cascader ref="cascader" v-model="threeLisData" @change="typeChaneg" :options="threeList"
          :props="{ checkStrictly: true, value: 'id', label: 'name' }" clearable>
        </el-cascader>
      </el-form-item>
      <el-form-item label="样本产地" prop="productOrigin">
        <el-input v-model="queryParams.productOrigin" placeholder="请输入样本产地" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="具体送样人" prop="sendPersonName">
        <el-input v-model="queryParams.sendPersonName" placeholder="请输入具体送样人" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="采样地址" prop="simpleTakeAddress">
        <el-input v-model="queryParams.simpleTakeAddress" placeholder="请输入采样地址" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="采样时间" prop="sampleTakeDate">
        <el-date-picker v-model="queryParams.sampleTakeDate" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
          :default-time="['00:00:00', '23:59:59']" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          :loading="exportLoading" v-hasPermi="['market:take-data:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="样本编码" align="center" prop="simpleCode" />
      <el-table-column label="主体名称" align="center" prop="marketInfoName" />
      <el-table-column label="样本类型" align="center" prop="simpleTypeName" />
      <el-table-column label="样本子类" align="center" prop="simpleTowLevelName" />
      <el-table-column label="样品名称" align="center" prop="simpleName" />
      <el-table-column label="采样时间" align="center" prop="sampleTakeDate" width="150">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.sampleTakeDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="样本产地" align="center" prop="productOrigin" />
      <el-table-column label="具体送样人" align="center" prop="sendPersonName" />
      <el-table-column label="送样人电话" align="center" prop="sendPersonPhone" />
      <el-table-column label="采样基数（千克）" align="center" prop="simpleTakeKg" />
      <el-table-column label="采样地址" align="center" prop="simpleTakeAddress" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" icon="el-icon-view" @click="openForm(scope.row.id)"
            v-hasPermi="['market:take-data:update']">查看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 对话框(添加 / 修改) -->
    <TakeDataForm ref="formRef" @success="getList" />
  </div>
</template>

<script>
import * as TakeDataApi from '@/api/market/takedata';
import TakeDataForm from './TakeDataForm.vue';
import * as AreaCategoryApi from '@/api/market/AreaCategory';
export default {
  name: "TakeData",
  components: {
    TakeDataForm,
  },
  data() {
    return {
      productList: [],
      parentList: [],
      threeList: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 样品采样列表
      list: [],
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 重新渲染表格状态
      refreshTable: true,
      // 选中行
      currentRow: {},
      threeLisData: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        simpleCode: null,
        marketInfoName: null,
        simpleTypeId: null,
        simpleTypeName: null,
        simpleTowLevelId: null,
        simpleTowLevelName: null,
        simpleId: null,
        simpleName: null,
        sampleTakeDate: [],
        sampleCheckDate: [],
        productOrigin: null,
        sendPersonName: null,
        simpleTakeKg: null,
        simpleLivePicture: null,
        signPicture: null,
        simpleTakeAddress: null,
        remark: null,
        dataSourceId: null,
        createTime: [],
      },
    };
  },
  created() {
    this.getList();
    this.othersList();
  },
  methods: {
    typeChaneg(value) {
      if (value.length == 1) {
        this.queryParams.simpleTypeId = value[0]
        this.queryParams.simpleTowLevelId = null
        this.queryParams.simpleId = null
      } else if (value.length == 2) {
        this.queryParams.simpleTowLevelId = value[1]
        this.queryParams.simpleId = null
        this.queryParams.simpleTypeId = null
      } else if (value.length == 3) {
        this.queryParams.simpleId = value[2]
        this.queryParams.simpleTypeId = null
        this.queryParams.simpleTowLevelId = null

      } else {
        this.queryParams.simpleTypeId = null
        this.queryParams.simpleTowLevelId = null
        this.queryParams.simpleId = null
      }
      console.log(value, '类型')
    },
    /** 查询列表 */
    async getList() {
      try {
        this.loading = true;
        const res = await TakeDataApi.getTakeDataPage(this.queryParams);

        this.list = res.data.list;
        this.total = res.data.total;
      } finally {
        this.loading = false;
      }
    },
    async othersList() {
      try {
        const threeList = await AreaCategoryApi.getCasCadeLevel();
        this.threeList = threeList.data
      } finally {
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.threeLisData = []
      this.queryParams.simpleTypeId = null
      this.queryParams.simpleTowLevelId = null
      this.queryParams.simpleId = null
      this.queryParams
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 添加/修改操作 */
    openForm(id) {
      this.$refs["formRef"].open(id);
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      const id = row.id;
      await this.$modal.confirm('是否确认删除样品采样编号为"' + id + '"的数据项?')
      try {
        await TakeDataApi.deleteTakeData(id);
        await this.getList();
        this.$modal.msgSuccess("删除成功");
      } catch { }
    },
    /** 导出按钮操作 */
    async handleExport() {
      await this.$modal.confirm('是否确认导出所有样品采样数据项?');
      try {
        this.exportLoading = true;
        const res = await TakeDataApi.exportTakeDataExcel(this.queryParams);
        this.$download.excel(res.data, '样品采样.xls');
      } catch {
      } finally {
        this.exportLoading = false;
      }
    },
  }
};
</script>
